//
//  ExceptionalVC.m
//  WanCaiC8
//
//  Created by tangang on 2018/5/2.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "ExceptionalVC.h"
#import "ExceptionalRecordCell.h"
#import "Top_upPriceCell.h"
#import "PlanVC.h"
#import "PlanVM.h"
#import "GradesModel.h"
#import "OnlineTop_up.h"
@interface ExceptionalVC ()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic ,strong) UITableView   *tableView;
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) UITextField       *payText;
@property (nonatomic ,strong) NSMutableArray    *priceArr;
@property (nonatomic ,strong) NSMutableArray    *dataArr;

@end

@implementation ExceptionalVC{
    NSInteger   selectPrice;
    NSInteger   selectType;
    NSString    *planId;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"打赏";
    [self headView];
    [self getData];
}

- (void)getData{
    WS(weakSelf);
    [[PlanVM manage] getUserExceptionalRecordWithUid:self.model.UserId withTop:10 WithLtype:[self.lotteryModel.lType integerValue] WithSuccess:^(id returnValue) {
        weakSelf.dataArr=returnValue;
        [weakSelf.tableView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
    
    [[PlanVM manage] getUserLastPlayWithUid:self.model.UserId withPlayName:@"全部" WithLtype:[self.lotteryModel.lType integerValue] WithSuccess:^(id returnValue) {
        NSMutableArray *arr=[GradesModel mj_objectArrayWithKeyValuesArray:[returnValue objectForKey:@"PageData"]];
        GradesModel *model=[arr firstObject];
        BetModel *bModel=[model.BettingRecord firstObject];
        planId=bModel.Id;
        [weakSelf.tableView reloadData];
    } andFail:^(NSString *errorMessage, NSError *error) {
        
    }];
    
}


- (void)headView{
    UIView *headView=[[UIView alloc]init];
    headView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:headView];
    [headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.height.mas_equalTo(@100);
    }];
    
    [headView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headView).with.offset(25);
        make.right.equalTo(headView).with.offset(-25);
        make.top.equalTo(headView).with.offset(8);
        make.height.mas_equalTo(@30);
    }];
    
    UIView *line1=[[UIView alloc]init];
    line1.backgroundColor=DEFAULTBGCOLOR;
    [headView addSubview:line1];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.collectionView.mas_bottom).with.offset(6);
        make.left.equalTo(headView).with.offset(10);
        make.right.equalTo(headView).with.offset(-10);
        make.height.mas_equalTo(@.7);
    }];
    
    QXLable *labTopUp=[[QXLable alloc]initWithFount:15];
    labTopUp.text=@"打赏金币:";
    [headView addSubview:labTopUp];
    [labTopUp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.collectionView);
        make.top.equalTo(line1).with.offset(15);
    }];
    
    QXLable *labUnits=[[QXLable alloc]initWithFount:15];
    labUnits.text=@"金币";
    labUnits.textAlignment=NSTextAlignmentRight;
    [headView addSubview:labUnits];
    [labUnits mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(labTopUp);
        make.right.equalTo(self.collectionView);
    }];
    
    _payText=[[UITextField alloc]init];
    _payText.text=@"20";
    _payText.keyboardType=UIKeyboardTypeNumberPad;
    _payText.font=[UIFont systemFontOfSize:15];
    _payText.textColor=DEFAULTTEXTBLACKCOLOR;
    [headView addSubview:_payText];
    [_payText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(labTopUp);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH-50, 30));
        make.left.equalTo(labTopUp.mas_right);
    }];
    
    UIButton  *btnExceptional=[[UIButton alloc]init];
    btnExceptional.backgroundColor=NAVCOLOR;
    btnExceptional.layer.masksToBounds=YES;
    btnExceptional.layer.cornerRadius=2;
    [btnExceptional addTarget:self action:@selector(onclickExceptional) forControlEvents:UIControlEventTouchUpInside];
    [btnExceptional setTitle:@"打赏" forState:UIControlStateNormal];
    [self.view addSubview:btnExceptional];
    [btnExceptional mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headView.mas_bottom).with.offset(15);
        make.left.equalTo(self.view).with.offset(35);
        make.right.equalTo(self.view).with.offset(-35);
        make.height.mas_equalTo(@38);
    }];
    
    [self.tableView reloadData];
}

#pragma mark -- 打赏
- (void)onclickExceptional{
    if ([self.payText.text integerValue]==0) {
        [SVProgressHUD showErrorWithStatus:@"请输入打赏金额"];
        return;
    }
    
    if (!planId) {
        return;
    }
    
    WS(weakSelf);
    [[PlanVM manage] giftCoinWithId:planId andCoin:self.payText.text WithSuccess:^(id returnValue) {
        [SVProgressHUD showSuccessWithStatus:@"打赏成功"];
        [weakSelf.navigationController popViewControllerAnimated:YES];
    } andFail:^(NSString *errorMessage, NSError *error) {
        if ([errorMessage intValue]==60018) {
            UIAlertController *alertCtrl=[UIAlertController alertControllerWithTitle:nil message:@"金币不足" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancleAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];;
            UIAlertAction *goAction=[UIAlertAction actionWithTitle:@"前往充值" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                OnlineTop_up *vc=[[OnlineTop_up alloc]init];
                [weakSelf.navigationController pushViewController:vc animated:YES];
            }];
            [alertCtrl addAction:cancleAction];
            [alertCtrl addAction:goAction];
            [weakSelf presentViewController:alertCtrl animated:YES completion:nil];
        }
    }];
}

#pragma mark -- UITableViewDelegate ,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExceptionalRecordCell *cell=[tableView dequeueReusableCellWithIdentifier:@"ExceptionalRecordCell" forIndexPath:indexPath];
    cell.model=self.dataArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    selectType=indexPath.row;
    [tableView reloadData];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    view.backgroundColor=[UIColor whiteColor];
    UIView *line=[[UIView alloc]initWithFrame:CGRectMake(13, 10, 2, 14)];
    line.backgroundColor=NAVCOLOR;
    [view addSubview:line];
    QXLable *lab=[[QXLable alloc]init];
    lab.text=@"近期打赏记录";
    [view addSubview:lab];
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(line);
        make.left.equalTo(line).with.offset(5);
    }];
    return view;
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.priceArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    Top_upPriceCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"Top_upPriceCell" forIndexPath:indexPath];
    [cell setTitle:self.priceArr[indexPath.row] andIsSelect:indexPath.row==selectPrice];
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    selectPrice=indexPath.row;
    _payText.text=self.priceArr[indexPath.row];
    [collectionView reloadData];
}

#pragma mark -- 懒加载
- (UITableView *)tableView{
    if (!_tableView) {
        WS(weakSelf);
        _tableView=[[UITableView alloc]init];
        _tableView.dataSource=self;
        _tableView.delegate=self;
        _tableView.backgroundColor=DEFAULTBGCOLOR;
        _tableView.estimatedRowHeight=50;
        _tableView.rowHeight=UITableViewAutomaticDimension;
        [_tableView registerClass:[ExceptionalRecordCell class] forCellReuseIdentifier:@"ExceptionalRecordCell"];
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf.view).with.insets(UIEdgeInsetsMake(180, 0, 0, 0));
        }];
    }
    return _tableView;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake((SCREEN_WIDTH-50-28*3-1)/4.0, 30)];
        [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 28.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator=NO;
        [_collectionView registerClass:[Top_upPriceCell class] forCellWithReuseIdentifier:@"Top_upPriceCell"];
    }
    return _collectionView;
}


- (NSMutableArray *)priceArr{
    if (!_priceArr) {
        _priceArr=[[NSMutableArray alloc]initWithObjects:@"20",@"100",@"500",@"1000", nil];
    }
    return _priceArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
